package com.example.fresh_market.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.fresh_market.R;
import com.example.fresh_market.adapter.AddressAdapter;
import com.example.fresh_market.constants.StrConstant;
import com.example.fresh_market.databinding.ActivityAddressBinding;
import com.example.fresh_market.entity.Address;
import com.example.fresh_market.request.ApiResponse;
import com.example.fresh_market.request.PageResult;
import com.example.fresh_market.utils.HttpUtil;
import com.example.fresh_market.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Map;

/**
 * 用户地址
 */
public class AddressActivity extends AppCompatActivity {

    private AddressAdapter addressAdapter;
    private ActivityAddressBinding binding;
    private Context mContext;
    private String token;
    private boolean createOrder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences preferences = getSharedPreferences(StrConstant.CONFIG, MODE_PRIVATE);
        token = preferences.getString(StrConstant.TOKEN, "");
        binding = ActivityAddressBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mContext = this;
        createOrder = getIntent().getBooleanExtra("createOrder", false);
        RecyclerView address_recycler = findViewById(R.id.address_list);
        address_recycler.setHasFixedSize(true);
        address_recycler.setLayoutManager(new LinearLayoutManager(mContext));
        addressAdapter = new AddressAdapter(new ArrayList<>(), new AddressAdapter.OnItemClickListener() {
            @Override
            public void onDeleteClick(View v, int position) {
                Address address = addressAdapter.getItem(position);
                HttpUtil.delete(mContext, "/usr/address/" + address.getId(), Map.of(HttpUtil.Auth, token), () -> {
                    ToastUtil.showToast(mContext, "删除成功");
                    addressAdapter.removeData(position);
                });
            }

            @Override
            public void onEditClick(View v, int position) {
                Address address = addressAdapter.getItem(position);
                Intent intent = new Intent(mContext, AddressEditActivity.class);
                intent.putExtra("address", new Gson().toJson(address));
                startActivity(intent);
            }

            @Override
            public void onDefaultClick(View v, int position) {
                Address address = addressAdapter.getItem(position);
                HttpUtil.get(mContext, "/usr/address/" + address.getId(), Map.of(HttpUtil.Auth, token), response -> {
                    ApiResponse apiResponse = new Gson().fromJson(response, ApiResponse.class);
                    if (apiResponse.getCode() == 200) {
                        ToastUtil.showToast(mContext, "设置成功");
                    }
                });
            }

            @Override
            public void onItemClick(View v, int position) {
                if (createOrder) {
                    Address address = addressAdapter.getItem(position);
                    Intent intent = new Intent(mContext, CreateOrderActivity.class);
                    intent.putExtra("address", new Gson().toJson(address));
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
        address_recycler.setAdapter(addressAdapter);
        initData();
        setEvent();
    }

    public void initData() {
        HttpUtil.get(mContext, "/usr/address/list?page=1&size=10000", Map.of(HttpUtil.Auth, token), response -> {
            ApiResponse<PageResult<Address>> apiResponse = new Gson().fromJson(response, new TypeToken<ApiResponse<PageResult<Address>>>() {
            }.getType());
            if (apiResponse.getCode() == 200) {
                addressAdapter.setData(apiResponse.getData().getList());
            }
        });
    }

    public void setEvent() {
        binding.addAddress.setOnClickListener(v -> {
            startActivity(new Intent(mContext, AddressEditActivity.class));
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
